package com.blt.govdata.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.blt.govdata.controller.base.BaseController;
import com.blt.govdata.model.TProjectLand;
import com.blt.govdata.service.ITProjectLandService;
import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 * @author AustinGJ123
 * @since 2018-03-26
 */
@RestController
@RequestMapping("/tProjectLand")
//todo 将所有的XXX修改为真实值
public class TProjectLandController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ITProjectLandService service;

    /**
     * 分页查询项目土地情况
     */
    @RequestMapping(value = "/selectProjectLandPage")
    public Map<String, Object> selectProjectLandPage(@RequestParam Map<String, Object> params, Page page) {
        Page<Map<String, Object>> result = service.selectProjectLandByPage(page, params);
        logger.info("分页查询土地情况");
        return ImmutableMap.of("status", "true", "data", result);
    }


    /**
     * 编辑土地利用
     */
    @RequestMapping(value = "/editProjectLand")
    public Map<String, Object> editProjectLand(TProjectLand tProjectLand) {
        if (null == tProjectLand || null == tProjectLand.getpId())
            return ImmutableMap.of("status", "false", "data", false);
        TProjectLand tProjectLandExist = this.service.selectOne(BeanUtils.instantiate(EntityWrapper.class).where("p_id = {0}",tProjectLand.getpId()));
        boolean b;
        if(null == tProjectLandExist) b = this.service.insert(tProjectLand);
        else{
            b = this.service.updateById(tProjectLand);
        }
        return ImmutableMap.of("status", "true", "data", b);
    }


    /**
     * 根据项目ID查询土地利用
     */
    @RequestMapping(value = "/selectProjectLandById", params = {"id"})
    public Map<String, Object> selectProjectLandById(Integer id) {
        List<TProjectLand> result = this.service.selectList(BeanUtils.instantiate(EntityWrapper.class).where("p_id = {0}",id));
        return ImmutableMap.of("status", "true", "data", result);
    }


    /**
     * 新增
     */
    @RequestMapping(value = "addPLand", params = {"pId"})
    public Map<String, Object> addPLand(TProjectLand Pland) {
        boolean result = this.service.insert(Pland);
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 修改
     */

    @RequestMapping(value = "editPLand", params = {"id"})
    public Map<String, Object> editPLand(TProjectLand Pland) {
        boolean result = this.service.updateById(Pland);
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 删除
     */

    @RequestMapping(value = "delete", params = {"id"})
    public Map<String, Object> delete(Integer id) {
        boolean result = this.service.deleteById(id);
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 新增或修改
     */
    @RequestMapping(value = "createOrModifyPLand", params = {"pId"})
    public Map<String, Object> createOrModifyPLand(TProjectLand Pland) {
        TProjectLand one = this.service.selectOne(BeanUtils.instantiate(EntityWrapper.class).where("p_id = {0}", Pland.getpId()).and("id = {0}",Pland.getId()));
        if (null == one) {
            return addPLand(Pland);
        }
        return editPLand(Pland);
    }


}
